﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;

namespace Photography
{
    public class Global : System.Web.HttpApplication
    {
        private log4net.ILog Logger { get { return log4net.LogManager.GetLogger(this.GetType()); } }

        void Application_Start(object sender, EventArgs e)
        {
            UrlUtilities.RegisterRoutes(RouteTable.Routes);

            //System.Console.SetOut(new System.IO.StreamWriter(Server.MapPath("~/Log/Trace.LOG")));
            log4net.Config.XmlConfigurator.Configure();
            Logger.Info("Application Started");
        }

        void Application_End(object sender, EventArgs e)
        {
            Logger.Info("Application Ended");
        }

        void Application_Error(object sender, EventArgs e)
        {
            if (Server.GetLastError() != null)
            {
                Logger.ErrorFormat("!!Error Occured!!\r\n{0}", Server.GetLastError().ToString());
            }
            else
            {
                Logger.Error("!!Unknwon Error Occured!!");
            }
            Server.Transfer("~/Error.aspx");
        }

        void Session_Start(object sender, EventArgs e)
        {
            Logger.InfoFormat("Sessions started for IP# {0}", Request.UserHostAddress);
            Session["IP"] = Request.UserHostAddress;
        }

        void Session_End(object sender, EventArgs e)
        {
            Logger.InfoFormat("Sessions Ended for IP# {0}", Session["IP"]);
        }
    }
}
